
/**
 * 导出文件通用方法
 * @param {*} response 响应数据
 * @param {*} defaultFileName 默认文件名
 */
 // utils/fileUtils.js
// src/utils/fileUtils.js
export const exportFile = (response, defaultFileName) => {
    try {
      const content = response.data
      const blob = new Blob([content])

      // 从响应头获取文件名
      let fileName = defaultFileName
      const disposition = response.headers['content-disposition']
      if (disposition && disposition.indexOf('attachment') !== -1) {
        const fileNameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
        const matches = fileNameRegex.exec(disposition)
        if (matches != null && matches[1]) {
          fileName = decodeURIComponent(matches[1].replace(/['"]/g, ''))
        }
      }
      // 创建下载链接
      const link = document.createElement('a')
      link.href = URL.createObjectURL(blob)
      link.download = fileName+'.xlsx'
      document.body.appendChild(link)
      link.click()
      
      // 清理资源
      document.body.removeChild(link)
      URL.revokeObjectURL(link.href)
    } catch (error) {
      console.error('文件导出失败:', error)
      throw new Error('文件导出失败，请重试')
    }
  }  